﻿//
// Copyright (C) 2010 Tarmin Rehve <tarmin.rehve@gmail.com>
//
//This file is part of the XDataGridView software library.
//
//    XDataGridView is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    XDataGridView is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Lesser License for more details.
//
//    You should have received a copy of the GNU Lesser Public License
//    along with XDataGridView.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;

namespace XControls
{
    /// <summary>
    /// This class is the base class for <see cref="XDataGridViewRowCursor"/> or <see cref="XDataGridViewColumnCursor"/> which help to highlight a column or a row line in a <see cref="XDataGridView"/> control
    /// Internal purpose only, this class should be not used directly.
    /// </summary>
    [DesignTimeVisible(false)]
    [Serializable]
    public class XDataGridViewCursor<DATAGRID_VIEW_BAND_COLLECTION>:
        Component
        where DATAGRID_VIEW_BAND_COLLECTION : System.Collections.IList, System.Collections.IEnumerable, System.Collections.ICollection
    {
        protected DATAGRID_VIEW_BAND_COLLECTION underlyingBandCollection;
        protected Color backColor;
        protected Color foreColor;
        protected Color previousBackColor;
        protected Color previousForeColor;
        protected int position;
        protected bool activated;

        private void Initialize()
        {
            backColor = Control.DefaultForeColor;
            foreColor = Control.DefaultBackColor;
            position = 0;
            activated = true;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XDataGridViewCursor"/> class.
        /// </summary>
        public XDataGridViewCursor()
        {
            underlyingBandCollection = default(DATAGRID_VIEW_BAND_COLLECTION);
            Initialize();
        }
        /*
        public XDataGridViewCursor(DATAGRID_VIEW_BAND_COLLECTION dataGridViewBandCollection)
        {
            if( dataGridViewBandCollection == null )
                throw new NullReferenceException( "a null reference is not allowed for the dataGridViewBandCollection parameter" );
            owningBandCollection = dataGridViewBandCollection;
            Initialize();
        }*/
        /// <summary>
        /// Moves the cursor from the previous band to the next band.
        /// </summary>
        /// <param name="previousBand">the previous band. It can be null if the previous band has been deleted or just after cursor creation</param>
        /// <param name="nextBand">the next band</param>
        protected void Move(DataGridViewBand previousBand, DataGridViewBand nextBand)
        {
            if (previousBand != null)
            {
                // restores colors of the band previously highlighted by the cursor
                previousBand.DefaultCellStyle.BackColor = previousBackColor;
                previousBand.DefaultCellStyle.ForeColor = previousForeColor;

                // saves colors of the next band to be highlighted
                previousBackColor = nextBand.DefaultCellStyle.BackColor;
                previousForeColor = nextBand.DefaultCellStyle.ForeColor;
            }

            if (activated)
            {
                // highlights the next band
                nextBand.DefaultCellStyle.BackColor = backColor;
                nextBand.DefaultCellStyle.ForeColor = ForeColor;
            }
        }
        /// <summary>
        /// Gets or sets the position of the cursor line in set of rows or columns of the <see cref="XDataGridView"/>
        /// Returns:
        ///     The index of the row or column highlighted by the cursor line.
        /// </summary>
        public int Position
        {
            get { return position; }
            set
            {
                if (underlyingBandCollection != null)
                {
                    if ((value < 0) || (value >= underlyingBandCollection.Count))
                        throw new IndexOutOfRangeException("Index " + value + " is not in the bounds of the collection [0," + (underlyingBandCollection.Count - 1) + "] !");

                    if ((position >= 0) && (position < underlyingBandCollection.Count))
                    {
                        // position is in the range of the owner row collection
                        Move((DataGridViewBand)underlyingBandCollection[position], (DataGridViewBand)underlyingBandCollection[value]);
                    }
                    else
                    {
                        // position is not in the range of the owner row collection
                        Move((DataGridViewBand)null, (DataGridViewBand)underlyingBandCollection[value]);
                    }
                }
                position = value;
            }
        }
        /// <summary>
        /// Update position of the cursor after bands addition or removal in the underlying bands collection.
        /// </summary>
        /// <param name="value">new position value</param>
        internal void UpdatePosition( int value )
        {
            position = value;
        }
        /// <summary>
        /// Gets or sets the background color for the cursor line
        /// Returns:
        ///     A <see cref="System.Drawing.Color"/> that represents the background color of the cursor.
        ///     The default is the value of the <see cref="System.Windows.Forms.Control.DefaultForeColor"/>
        ///     property.
        /// </summary>
        public Color BackColor
        {
            get { return backColor; }
            set { backColor = value; }
        }
        /// <summary>
        /// Gets or sets the foreground color for the cursor line.
        /// Returns:
        ///     A <see cref="System.Drawing.Color"/> that represents the foreground color of the cursor.
        ///     The default is the value of the <see cref="System.Windows.Forms.Control.DefaultBackColor"/>
        ///     property.
        /// </summary>
        public Color ForeColor
        {
            get { return foreColor; }
            set { foreColor = value; }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the changes involved by the cursor are activated
        /// </summary>
        public bool Activated
        {
            get { return activated; }
            set
            {
                activated = value;
                if (activated )
                    if (underlyingBandCollection != null )
                        if( (position >= 0) && (position < underlyingBandCollection.Count) )
                            Move((DataGridViewBand)underlyingBandCollection[position], (DataGridViewBand)underlyingBandCollection[position]);
            }
        }

        /// <summary>
        /// Gets the instance of <see cref="DataGridViewRowCollection"/> or <see cref="DataGridViewColumnCollection"/> beneath the cursor
        /// </summary>
        [Browsable(false)]
        public DATAGRID_VIEW_BAND_COLLECTION UnderlyingBandCollection
        {
            get { return underlyingBandCollection; }
            internal set { underlyingBandCollection = value; }
        }
    };

    /// <summary>
    /// Class to handle a column cursor in a <see cref="XDataGridView"/>.
    /// </summary>
    [Serializable]
    public class XDataGridViewColumnCursor : XDataGridViewCursor<DataGridViewColumnCollection>{};

    /// <summary>
    /// Class to handle a row cursor in a <see cref="XDataGridView"/>.
    /// </summary>
    [Serializable]
    public class XDataGridViewRowCursor : XDataGridViewCursor<DataGridViewRowCollection> { };
}
